<?php
/**
 * @annotate 微信功能类
 * @group   Admin/Company
 * @author   yachtxiao <fuck@fackyou.org>
 */
class WeixinAction extends BaseAction {
	protected $aVerify = array(
   	'reply','add_reply_word','add_reply_image','doadd_reply_image_simple','edit_reply_word','edit_reply_image','save_reply_image_simple','doadd_reply_image_more','save_reply_image_more','custommenu',
   	'add_menu','doadd_menu','edit_menu','save_menu','edit_menu_data','save_menu_data','del_menu','uploadpic','addelement','del_reply','accesswx','save_accesswx',
   	'push_custommenu'
  	);//需要验证的方法
	public function reply(){
		$reply = D('Weixin')->get_reply();
		$this->assign('reply',$reply);
		$this->display();
	}
	
	public function add_reply_word(){
		$data = $_POST;
		$result = D('Weixin')->add_reply_word($data);
		if($result){
			$this->success('添加成功','__URL__/reply');
		}else{
			$this->error('添加失败','__URL__/reply');
		}
	}
	
	public function add_reply_image(){
		$type = $_GET['type'];
		if($type == 1){
			$this->display('add_reply_image_simple');
		}else{
			$this->display('add_reply_image_more');
		}
	}
	
	public function doadd_reply_image_simple(){
		$data = $_POST;
		$result = D('Weixin')->add_reply_image_simple($data);
		if($result){
			$this->success('添加成功','__URL__/reply');
		}else{
			$this->error('添加失败','__URL__/reply');
		}
	}
	
	public function edit_reply_word(){
		$reply_id = $_POST['reply_id'];
		$data = $_POST;
		$result = D('Weixin')->edit_reply_word($data,$reply_id);
		if($result){
			$this->success('修改成功','__URL__/reply');
		}else{
			$this->error('修改失败','__URL__/reply');
		}
	}
	
	public function edit_reply_image(){
		$reply_id = $_GET['reply_id'];
		$reply = D('Weixin')->get_reply_image($reply_id);
		$this->assign('reply',$reply);
		if($reply['type']==1){
			$this->display('edit_reply_image_simple');
		}else{
			$this->display('edit_reply_image_more');
		}
	}
	
	public function save_reply_image_simple(){
		$reply_id = $_GET['reply_id'];
		$data = $_POST;
		$result = D('Weixin')->save_reply_image_simple($data,$reply_id);
		if($result){
			$this->success('修改成功','__URL__/reply');
		}else{
			$this->error('修改失败','__URL__/reply');
		}
	}
	
	public function doadd_reply_image_more(){
		$data = $_POST;
		$result = D('Weixin')->add_reply_image_more($data);
		if($result){
			$this->success('添加成功','__URL__/reply');
		}else{
			$this->error('添加失败','__URL__/reply');
		}
	}
	
	public function save_reply_image_more(){
		$reply_id = $_GET['reply_id'];
		$data = $_POST;
		$result = D('Weixin')->save_reply_image_more($data,$reply_id);
		if($result){
			$this->success('修改成功','__URL__/reply');
		}else{
			$this->error('修改失败','__URL__/reply');
		}
	}
	
	public function custommenu(){
		$menu = D('Weixin')->get_menu_list();
		$this->assign('menu',$menu);
		$this->display();
	}
	
	public function add_menu(){
		$fid = $_POST['fid'];
		$url = empty($fid)?U('Weixin/doadd_menu'):U('Weixin/doadd_menu',array('fid'=>$fid));
		$this->assign('url',$url);
		$this->display();
	}
	
	public function doadd_menu(){
		$data = $_POST;
		$data['fid'] = empty($_GET['fid'])?0:$_GET['fid'];
		$result = D('Weixin')->add_menu($data);
		if($result){
			$this->success('添加成功','__URL__/custommenu');
		}else{
			$this->error('添加失败','__URL__/custommenu');
		}
	}
	
	public function edit_menu(){
		$mid = $_POST['mid'];
		$url = empty($mid)?U('Weixin/save_menu'):U('Weixin/save_menu',array('mid'=>$mid));
		$menu = D('Weixin')->get_menu($mid);
		$this->assign('menu',$menu);
		$this->assign('url',$url);
		$this->display();
	}
	
	public function save_menu(){
		$data = $_POST;
		$data['mid'] = $_GET['mid'];
		$result = D('Weixin')->save_menu($data);
		if($result){
			$this->success('修改成功','__URL__/custommenu');
		}else{
			$this->error('修改失败','__URL__/custommenu');
		}
	}
	
	public function edit_menu_data(){
		$mid = $_POST['mid'];
		$menu = D('Weixin')->get_menu($mid);
		if($menu['fid']==0){
			$submenus = D('Weixin')->get_menu_byfid($mid);
			if(empty($submenus)){
				$submenu = 0;
			}else{
				$submenu = 1;
			}
		}else{
			$submenu = 0;
		}
		$this->assign('submenu',$submenu);
		$this->assign('menu',$menu);
		$this->display();
	}
	
	public function save_menu_data(){
		$data = $_POST;
		$data['mid'] = $_GET['mid'];
		$result = D('Weixin')->save_menu_data($data);
		if($result){
			$this->success('修改成功','__URL__/custommenu');
		}else{
			$this->error('修改失败','__URL__/custommenu');
		}
	}
	
	public function del_menu(){
		$mid = $_POST['mid'];
		$result = D('Weixin')->del_menu($mid);
		if($result){
			$this->ajaxReturn('','删除成功',1);
		}else{
			$this->ajaxReturn('','删除失败',0);
		}
	}
		
	public function uploadpic(){
		//$data = $_FILES;
		$aid = upload_img_one('weixin',$_FILES['fileToUpload']);
		$return = array(
						'info' => get_img_url($aid),
						'aid' => $aid
					);
		exit(json_encode($return));
	}
	
	public function addelement(){
		$num = $_POST['num'];
		$top = $_POST['top'];
		$this->assign('num',$num);
		$this->assign('top',$top);
		$this->display('reply_image_more_template');
	}

	public function del_reply(){
		$data['reply_id'] = $_POST['rid'];
		$map['is_del'] = 1;
		$ba = D('weixin_reply')->where($data)->save($map);
			if($ba){
				$this->ajaxReturn('','success',1);
			}else{
				$this->ajaxReturn('','error',0);
			}
	}
	
	public function accesswx(){
		$accesswx = D('Weixin')->get_accesswx();
		$wxurl = 'http://www.rencailaile.com/weixin.php?m=Companywx&a=access&cid='.$_SESSION['company']['cid'];
		//$wxurl = U('Companywx/access');
		$access_token = md5($_SESSION['company']['cid']);
		$this->assign('wxurl',$wxurl);
		$this->assign('access_token',$access_token);
		$this->assign('accesswx',$accesswx);
		$this->display();
	}
	
	public function save_accesswx(){
		$data = $_POST;
		$result = D('Weixin')->save_accesswx($data);
		if($result){
			$this->success('修改成功','__URL__/accesswx');
		}else{
			$this->error('修改失败','__URL__/accesswx');
		}
	}
	
	public function push_custommenu(){
		$menu = D('Weixin')->get_menu_list();
		$result = D('Weixin')->push_menu($menu);
		if($result==1){
			$this->success('发布成功','__URL__/custommenu');
		}elseif($result ==2){
			$this->error('发布失败','__URL__/custommenu');
		}else{
			$this->error('菜单设置有空值，请补满','__URL__/custommenu');
		}
	}

}
?>